<template>
    <div>
        <div id="echarThree" ref="echarThree"></div>
    </div>
</template>
<script>
import {
    getNeedTakeEggs,
    getNeedPictureEgg,
    getNeedCheckDoves,
    getMatEggsOfYesterday,
    getAbnormalVoOfYesterday,
} from "../../../api/Dovecote/InfoManage/dataCount.js";
export default {
    name: "echarThree",
    created() {
        if (this.currentDovecote.baseId) {
            this.getNeedPictureEggNum(); //今日需照蛋数量
            this.getNeedTakeEggsNum(); //今日需抽蛋数量
            this.getNeedCheckDovesNum(); //今日需要查仔数量
            this.getMatEggsOfYesterdayNum(); //昨日垫蛋数量
            this.getAbnormalVoOfYesterdayNum(); //昨日异常数量
        }
    },
    data() {
        return {
            //配置
            option: {
                title: {
                    text: "数量分析：",
                    show: "true",
                    textStyle: {
                        fontWight: "normal",
                    },
                    left: "center",
                },
                series: {
                    type: "pie",
                    data: [],
                    emphasis: {
                        itemStyle: {
                            color: "pink",
                        },
                    },
                    label: {
                        formatter(arg) {
                            //formatter可以设置显示什么内容，利用它的回调函数
                            // console.log(arg);
                            return arg.name + "占比" + arg.percent;
                        },
                    },
                    // radius: 20  //设置半径大小， 具体数值/百分比
                    radius: "20%", //百分比是占，饼图所在容器的宽高中，小的部分的一半的百分比
                    radius: ["20%", "60%"],
                    roseType: "radius", //南丁格尔图形式，让每一部分的半径不同，半径大小由其所占比例决定
                    selectedMode: "single", //选中效果，single是让它分离，不选中时复原
                    selectedMode: "multiple", // 是让它分离，不选中时不复原
                    selectedOffset: 30, //选中后分离的距离
                },
            },
            //图表
            echarThree: "",
            //获取照蛋数量所需参数
            needPictureEggParams: {
                baseId: "",
                dovecoteNumber: "",
            },
            //获取抽蛋数量所需参数
            needTakeEggsParams: {
                baseId: "",
                dovecoteNumber: "",
            },
            //获取查仔数量所需参数
            needCheckDovesParams: {
                baseId: "",
                dovecoteNumber: "",
            },
            //获取垫蛋数量所需参数
            matEggsOfYesterdayParams: {
                baseId: "",
                dovecoteNumber: "",
            },
            //昨日异常数量所需参数
            abnormalVoOfYesterdayParams: {
                baseId: "",
                dovecoteNumber: "",
            },
            //需要抽蛋的数量
            needTakeEggsNum: "",
            //需要照蛋的数量
            needPictureEggNum: "",
            //需要查仔数量
            needCheckDovesNum: "",
            //昨日垫蛋数量
            matEggsOfYesterdayNum: "",
            //昨日异常数量
            abnormalVoOfYesterdayNum: "",
            //昨日异常数量最终数据
            fiAbnormalVoOfYesterdayNum: "",
            allData: [], //各种异常数据得到的数组
        };
    },
    computed: {
        // ...mapState({
        //     currentDovecote: (
        //         state //当前鸽棚信息
        //     ) => state.Usersetting.CurrentDovecote,
        // }),
        currentDovecote: function () {
            return this.$store.state.Usersetting.CurrentDovecote;
        },
    },
    watch: {
        allData: {
            deep: true,
            handler(newValue, oldValue) {
                if (newValue.length === 5) {
                    this.option.series.data = this.allData;
                    this.echarThree.setOption(this.option);
                }
            },
        },
    },
    mounted() {
        this.initCharts();
    },
    methods: {
        //设置图表
        initCharts() {
            this.$nextTick(() => {
                this.echarThree = this.$echarts.init(this.$refs.echarThree);
            });
        },
        //今日需抽蛋数量
        async getNeedTakeEggsNum() {
            // console.log("我在获取抽蛋数量");
            this.needTakeEggsParams.baseId = this.currentDovecote.baseId;
            this.needTakeEggsParams.dovecoteNumber =
                this.currentDovecote.dovecoteNumber;
            // console.log("鸽棚数据", this.currentDovecote);
            // console.log("照蛋参数", this.needTakeEggsParams);

            let { data } = await getNeedTakeEggs(this.needTakeEggsParams);
            // console.log(data);
            // console.log("后执行");

            if (data.statusCode === 20000) {
                this.needTakeEggsNum = data.data;
                let dataTemp = {
                    name: "今日抽蛋",
                    value: data.data,
                };
                this.allData.push(dataTemp);
            }
        },

        //今日需照蛋数量
        async getNeedPictureEggNum() {
            let that = this;
            // console.log(that.needPictureEggParams)
            that.needPictureEggParams.baseId = that.currentDovecote.baseId;
            that.needPictureEggParams.dovecoteNumber =
                that.currentDovecote.dovecoteNumber;

            let { data } = await getNeedPictureEgg(that.needPictureEggParams);
            // console.log("我在获取照蛋数量");
            // console.log(data);

            if (data.statusCode === 20000) {
                that.needPictureEggNum = data.data;
                let dataTemp = {
                    name: "今日照蛋",
                    value: data.data,
                };
                this.allData.push(dataTemp);
            }
        },

        //今日需要查仔数量
        async getNeedCheckDovesNum() {
            let that = this;
            this.needCheckDovesParams.baseId = this.currentDovecote.baseId;
            this.needCheckDovesParams.dovecoteNumber =
                this.currentDovecote.dovecoteNumber;
            // console.log("数据", this.currentDovecote);
            // console.log("查仔参数", that.needCheckDovesParams);

            let { data } = await getNeedCheckDoves(that.needCheckDovesParams);
            // console.log("我在获取查仔数量", data);
            // console.log();

            if (data.statusCode === 20000) {
                that.needCheckDovesNum = data.data;
                let dataTemp = {
                    name: "今日查仔",
                    value: data.data,
                };
                this.allData.push(dataTemp);
            }
        },

        //昨日垫蛋数量
        async getMatEggsOfYesterdayNum() {
            let that = this;

            this.matEggsOfYesterdayParams.baseId = this.currentDovecote.baseId;
            this.matEggsOfYesterdayParams.dovecoteNumber =
                this.currentDovecote.dovecoteNumber;

            let { data } = await getMatEggsOfYesterday(
                this.matEggsOfYesterdayParams
            );
            // console.log("我在获取垫蛋数量");
            // console.log(data);

            if (data.statusCode === 20000) {
                this.matEggsOfYesterdayNum = data.data;
                let dataTemp = {
                    name: "今日垫蛋",
                    value: data.data,
                };
                this.allData.push(dataTemp);
            }
        },

        //昨日异常数量
        async getAbnormalVoOfYesterdayNum() {
            this.abnormalVoOfYesterdayParams.baseId =
                this.currentDovecote.baseId;
            this.abnormalVoOfYesterdayParams.dovecoteNumber =
                this.currentDovecote.dovecoteNumber;

            let { data } = await getAbnormalVoOfYesterday(
                this.abnormalVoOfYesterdayParams
            );
            // console.log("我在获取异常数量");
            // console.log(data);

            if (data.statusCode === 20000) {
                // console.log(data.data);
                this.abnormalVoOfYesterdayNum = data.data; //要进行处理，最终数据是fiAbnormalVoOfYesterdayNum
                this.fiAbnormalVoOfYesterdayNum = 0;

                this.abnormalVoOfYesterdayNum.forEach((item) => {
                    //获取每种异常的数量进行累加
                    this.fiAbnormalVoOfYesterdayNum += item.amount;
                });
                // console.log(this.fiAbnormalVoOfYesterdayNum);
                let dataTemp = {
                    name: "昨日异常",
                    value: this.fiAbnormalVoOfYesterdayNum,
                };
                this.allData.push(dataTemp);
            }
        },
    },
};
</script>
<style lang="scss" scoped>
#echarThree {
    width: 580px;
    height: 350px;
}
</style>